package org.rency.crawler.fetch.handler.impl;

import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.rency.crawler.common.exception.CrawlerException;
import org.rency.crawler.domain.state.FetchStatus;
import org.rency.crawler.fetch.handler.AbstractFetchHandler;
import org.rency.crawler.fetch.helper.FetchCarrier;
import org.rency.crawler.fetch.repository.FetchPageRepository;
import org.springframework.stereotype.Component;

/**
 * 名称:FetchAnalysisHandler
 * 描述:${TODO}
 *
 * @author:rency
 * @version:V1.0.0
 * @JDK 1.8
 * @since 2017/11/22 22:21
 */
@Component
public class FetchAnalysisHandler extends AbstractFetchHandler{

  @Resource
  private FetchPageRepository     fetchPageRepository;

  @Override
  protected String getHandlerName() {
    return "内容分析";
  }

  @Override
  protected void onHandler(FetchCarrier carrier) throws CrawlerException {
    try {
      if (CollectionUtils.isNotEmpty(carrier.getDocQueueCollcetion())) {
        fetchPageRepository.save(carrier.getDocQueueCollcetion());
      }
    }catch (Exception e){
      logger.error("执行{}异常.",this.getHandlerName(),e);
    }
    carrier.getFetchState().setFsStatus(FetchStatus.SUCCESS);
  }
}
